package com.hs.hlw.credit.system.web.controller.api;

import com.hs.hlw.credit.system.business.dto.ResponseDto;
import com.hs.hlw.credit.system.business.service.DataSearchService;
import com.hs.hlw.credit.system.common.util.JsonUtil;
import com.hs.hlw.credit.system.data.DataUtils;
import com.hs.hlw.credit.system.data.mybatis.Pageable;
import com.hs.hlw.credit.system.data.primary.model.SubmissionFormField;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * DataSearchController
 * @author zhb
 * @date 2018/11/26
 */
@RestController
@RequestMapping("/api/data/search")
public class DataSearchController {
    private @Autowired DataSearchService dataSearchService;

    @GetMapping(path = "searchSubmissionDetail")
    @ApiOperation(value = "查询报送数据详情", notes = "查询报送数据详情")
    public ResponseDto searchSubmissionDetail(@RequestParam @ApiParam(value = "表单ID", required = true) Integer formId,
                                              @RequestParam @ApiParam(value = "数据id", required = true) Integer ID) {
        return dataSearchService.searchSubmissionDetail(formId, ID);
    }

    @GetMapping(path = "searchSubmissionDetailByBatch")
    @ApiOperation(value = "通过批次查询报送数据详情", notes = "通过批次查询报送数据详情")
    public ResponseDto searchSubmissionDetailByBatch(@RequestParam @ApiParam(value = "批次ID", required = true) Integer batchId) {
        return dataSearchService.searchSubmissionDetailByBatch(batchId);
    }

    @GetMapping(path = "searchSubmissionData")
    @ApiOperation(value = "查询报送数据列表", notes = "查询报送数据列表")
    public ResponseDto searchSubmissionData(@RequestParam @ApiParam(value = "表单ID", required = true) Integer formId,
                                            @RequestParam(required = false) @ApiParam(value = "状态(0:草稿, 1:非草稿)") Short status,
                                            @RequestParam(required = false) @ApiParam(value = "查询页码", defaultValue = "1") Integer page,
                                            @RequestParam(required = false) @ApiParam(value = "每页数据条数", defaultValue = "10") Integer size,
                                            @RequestParam(required = false) @ApiParam(value = "查询条件,使用json数组,拥有属性有:" +
                                                  "fieldCode(字段名称),fieldValue(字符值),logicOperator(逻辑运算符)," +
                                                  "logicRelationship(逻辑连接符, and表示且, or表示或, 第一个可以不用传)") String condition,
                                            @RequestParam(required = false, defaultValue = "ID") @ApiParam(value = "排序字段（多个用逗号隔开）", defaultValue = "ID") String orderField,
                                            @RequestParam(required = false, defaultValue = "desc") @ApiParam(value = "排序规则（asc:升序，desc:降序，多个用逗号隔开）", defaultValue = "desc") String direction) {
        Pageable pageable = DataUtils.pageable(page, size, orderField, direction);
        List<SubmissionFormField> whereList = new ArrayList<>();
        if (status != null) {
            SubmissionFormField field = new SubmissionFormField();
            field.setFieldCode("ADD_DATA_STATUS");
            field.setFieldValue(status.toString());
            field.setLogicOperator((short) 1);
            field.setFieldDataType("VARCHAR");
            whereList.add(field);
        }
        List<SubmissionFormField> list = JsonUtil.fromJsonArray(condition, SubmissionFormField.class);
        if (list != null) {
            whereList.addAll(list);
            if (whereList.size() > list.size()) {
                whereList.get(1).setLogicRelationship("and");
            }
        }
        ResponseDto responseDto = dataSearchService.searchSubmissionData(formId, whereList, pageable);
        return responseDto;
    }
}